package com.infinitegroup.findmystuff.models;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.infinitegroup.findmystuff.presenters.Constants;

import android.content.Context;

/**
 * Used only by admins. Establishes an explicit link to the items table in the database. Used in tandem with an account link. 
 * @author Tri-An
 *
 */
public class ItemLink implements Item{
	
	private DatabaseLink dblink;
	
	/**
	 * Constructor
	 * @param context
	 */
	public ItemLink(Context context) {
		dblink = new DatabaseLink(context);
	}

	/**
	 * Insert a new item into the database based on inputs. Invalid inputs are handled in presenter.
	 */
	public long insertNewItem(String itemname, String location, String reward, String description, 
			String status, String category, String username) {
		dblink.open();
		long id = dblink.insertItem(itemname, location, reward, description, status, category, username);
		dblink.close();
		return id;
	}

	/**
	 * Get an item from the database.
	 */
	public Map<String, String> getItem(String itemId) {
		List<Map<String,String>> l;
		dblink.open();
		l = dblink.getItems(Constants.ITEM_ID, new String[] {itemId}, null);
		dblink.close();
		Map<String,String> m = null;
		if ( !l.isEmpty() ){
			m = l.get(0); // there's only going to be one item since id is unique.
		}
		return m;
	}

	/**
	 * Get a list of items filtered by inputs.
	 */
	public List<Map<String, String>> getItemsFilteredBy(String category,
			String status, boolean date) {
		List<Map<String,String>> items;
		Object[] info = generateAttributeString(category, status);
		String attributes = null;
		String[] values= null;
		if (info!=null){
			attributes = (String) info[0];
			values = (String[]) info[1];
		}
		System.out.println("" + attributes + " " + Arrays.toString(values));
		dblink.open();
		items = dblink.getItems(attributes, values, Constants.ORDERBY_DATE_DESCENDING);
		dblink.close();
		return items;
	}
	
	/**
	 * Create a string the lists the attributes of an item.
	 * @param category
	 * @param status
	 * @return
	 */
	private Object[] generateAttributeString(String category, String status){
		String attributes = "";
		String[] values= null; //must be initialized. really sad.
		if (category!=null){
			values = new String[] {category};
			attributes += Constants.ITEM_CATEGORY + " = ?";
			if (status!=null){
				attributes += " AND " + Constants.ITEM_STATUS + " = ?";
				values = new String[] {category, status};
			}
		} 
		else {
			if (status!=null){
				attributes += Constants.ITEM_STATUS + " = ?";
				values = new String[] {status};
			}
		}
		if (attributes.equals("")){
			return null;
		} 
		else {
			return new Object[] {attributes, values};
		}
	}
	
	/**
	 * Create a query string for use in the database.
	 * @param arguments Arguments for the query
	 * @return
	 */
	private Object[] generateQueryStrings(Map<String, String> arguments) {
		String attributes = "";
		String[] values = new String[arguments.size()];
		int i = 0;
		for (String key : arguments.keySet()){
			attributes += key +" = ?";
			values[i] = arguments.get(key);
			i++;
			if (i<arguments.size()){
				attributes += " AND ";
			}
		}
		return new Object[] {attributes, values};
	}

	/**
	 * Gets the items filtered by the inputed parameters.
	 */
	public List<Map<String, String>> getItemsFilteredBy(String itemname,
			String location, String status, String category, String orderBy) {
		Map<String, String> m = new HashMap<String, String>();
		List<Map<String,String>> items;
		if (itemname != null) m.put(Constants.ITEM_ITEMNAME, itemname);
		if (location != null) m.put(Constants.ITEM_LOCATION, location);
		if (status != null) m.put(Constants.ITEM_STATUS, status);
		if (category != null) m.put(Constants.ITEM_CATEGORY, category);
		Object[] queries = generateQueryStrings(m);
		String attributes = (String) queries[0];
		String[] values = (String[]) queries[1];
		dblink.open();
		items = dblink.getItems(attributes, values, orderBy);
		dblink.close();
		return items;
	}
}
